package com.richstonedt.smartcity.xaytsccommonrs.controller;

import com.richstonedt.smartcity.xaytsccommoncs.annotation.ObsClientAnnotation;
import com.richstonedt.smartcity.xaytsccommoncs.service.FileStorageService;
import com.richstonedt.smartcity.xaytsccommonms.model.FileStorage;
import com.richstonedt.smartcity.xaytsccommonms.params.BatchUrlQueryParams;
import com.richstonedt.smartcity.xaytsccommonms.params.BatchUrlQueryPerfectParams;
import com.richstonedt.smartcity.xaytsccommonms.params.FileUploadParams;
import com.richstonedt.smartcity.xaytsccommonms.params.ObsFileCommonParams;
import com.richstonedt.smartcity.xaytsccommonms.vo.Result;
import com.richstonedt.smartcity.xaytsccommonrs.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <b><code>ObsFileController</code></b>
 * <p/>
 * Description
 * <p/>
 * <b>Creation Time:</b> 2022/3/29 15:01:10
 *
 * @author tangjiahuan
 * @since xaytsc-common-be
 */
@RestController
@RequestMapping("/common-file")
public class ObsFileController {

    private final FileStorageService fileStorageService;

    public ObsFileController(FileStorageService fileStorageService) {
        this.fileStorageService = fileStorageService;
    }

    /**
     * 文件上传
     * @param params 文件上传参数集合
     * @return 文件记录对象
     */
    @ApiOperation("上传文件")
    @PostMapping("/uploadFile")
    public Result<List<FileStorage>> uploadFile(@Validated FileUploadParams params) {
        List<FileStorage> upload = fileStorageService.upload(params);
        return ResultUtil.data(upload);
    }

    /**
     * 下载文件
     * @param params obs文件操作通用参数
     * @param response 响应对象
     */
    @ApiOperation("下载文件")
    @PostMapping("/downloadFile")
    public void downloadFile(@Validated ObsFileCommonParams params,
                             HttpServletResponse response) {
        fileStorageService.downloadObject(params,response);
    }

    /**
     * 删除文件
     * @param params obs文件操作通用参数
     */
    @ApiOperation("删除文件")
    @DeleteMapping("/deleteFile")
    public Result<String> deleteFile(@Validated ObsFileCommonParams params)  {

        Boolean b = fileStorageService.deleteFile(params);
        if(b){
            return ResultUtil.success(params.getObjectKey());
        }
        return ResultUtil.error("删除失败!");
    }

    /**
     * 批量删除
     * @param bucketName 桶名
     * @param objectKeys 对象key结合
     */
    @ApiOperation("批量删除文件")
    @ObsClientAnnotation
    @DeleteMapping("/deleteBatchFiles")
    public Result<List<String>> deleteBatchFiles(@ApiParam(value = "桶名", required = true) @RequestParam("bucketName") String bucketName,
                                                 @ApiParam(value = "文件在桶内路径", required = true) @RequestParam("baseUrl") String baseUrl,
                                     @ApiParam(value = "对象名", required = true) @RequestParam("objectKeys") List<String> objectKeys)  {

        List<String> s = fileStorageService.deleteBatchFiles(bucketName,baseUrl, objectKeys);
        return ResultUtil.data(s);

    }

    @ApiOperation("获取临时访问授权url")
    @GetMapping("/getTemporarySignatureUrl")
    public Result<String> getTemporarySignatureUrl(@Validated ObsFileCommonParams params){

        String temporarySignatureUrl = fileStorageService.getTemporarySignatureUrl(params);
        return ResultUtil.data(temporarySignatureUrl);
    }

    @ApiOperation("批量获取临时访问授权URL")
    @GetMapping("/batchTemporarySignatureUrl")
    public Result<Map<String, String>> batchTemporarySignatureUrl(@Validated BatchUrlQueryParams params){

        Map<String, String> map = fileStorageService.batchTemporarySignatureUrl(params);
        return ResultUtil.data(map);
    }

    @ApiOperation("批量获取临时访问授权URL(支持不同文件夹下图片批量获取)")
    @PostMapping("/batchTemporarySignatureUrlPerfect")
    public Result<Map<String, String>> batchTemporarySignatureUrlPerfect(@RequestBody @Validated BatchUrlQueryPerfectParams params){

        Map<String, String> map = fileStorageService.batchTemporarySignatureUrlPerfect(params);
        return ResultUtil.data(map);
    }

    /**
     * 处理图片并下载
     *
     * @param bucketName 桶名
     * @param objectKey  对象key
     * @throws IOException 抛出异常
     */
 /*   @ApiOperation("处理图片并下载")
    @GetMapping("/handlePicture")
    public void uploadFile(@ApiParam(value = "桶名", required = true) @RequestParam("bucketName") String bucketName,
                           @ApiParam(value = "对象名", required = true) @RequestParam("objectKey") String objectKey) throws IOException {

    }*/

    /*@ApiOperation("测试")
    @GetMapping("/test")
    @ObsClientAnnotation
    public String test(@ApiParam(value = "桶名", required = true) @RequestParam("bucketName") String bucketName,
                       @ApiParam(value = "对象名", required = true) @RequestParam("objectKey") String objectKey){
        obsUtil.setBucketName(bucketName);
        TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, 1000000);
        request.setBucketName(bucketName);
        request.setObjectKey(objectKey);

// 设置图片转码参数

        Map<String,Object> queryParams = new HashMap<String, Object>();
        queryParams.put("x-image-process", "image/resize,m_fixed,w_100,h_100/rotate,100");
        request.setQueryParams(queryParams);

        TemporarySignatureResponse response = obsUtil.getObsClient().createTemporarySignature(request);
//获取支持图片转码的下载链接
        System.out.println("Getting object using temporary signature url:");
        System.out.println("\t" + response.getSignedUrl());
        return response.getSignedUrl();
    }*/
}